package chatclient.applets;

import chatclient.netio.Member;
import chatclient.netio.Message;
import chatclient.applets.note.Note;
import chatclient.applets.canvas.ListCanvas;
import chatclient.tools.NoteContainer;
import java.awt.*;
import java.util.Enumeration;

public class ListView extends ChatView
        implements NoteContainer {

    private ListPanel listPanel;

    @Override
    protected void initChatPanel() {
        listPanel = new ListPanel(this);
        chatPanel = listPanel;
    }

    @Override
    public void init() {

        super.init();

        if (suspendKey == null) {
            suspendKey = "list";
        }
    }

    @Override
    public String getMemberOrder(Member member) {
        StringBuffer stringbuffer = new StringBuffer();
        String s = getParameter("order") != null ? getParameter("order") : "";
        Message.expand(s, member.getAttrs(), new StringBuffer[]{
                    stringbuffer
                }, Message.ESC_NONE, null);
        return stringbuffer.toString();
    }

    @Override
    public void addedMember(Member member, int i) {
        ListCanvas canvas = listPanel.getListCanvas();
        canvas.addNote(createMemberNote(canvas, member), i);
        listPanel.updateBar();
    }

    @Override
    public void removedMember(Member member, int i) {
        ListCanvas canvas = listPanel.getListCanvas();
        canvas.removeNote(i);
        listPanel.updateBar();
    }

    @Override
    public void updatedMember(Member member, int i, int j) {
        ListCanvas canvas = listPanel.getListCanvas();        
        Note note = createMemberNote(canvas, member);
        if (j == i) {
            canvas.replaceNote(note, i);
        } else {
            canvas.removeNote(i);
            canvas.addNote(note, j);
        }
        listPanel.updateBar();
        String s = getChat().getSelectedNick();
        if (s != null && member.getName().equalsIgnoreCase(s)) {
            listPanel.updateUserPanel(s);
        }
        if (member.getName().equalsIgnoreCase(getMyName())) {
            listPanel.layoutUserPanel();
        }
    }

    @Override
    public void refreshedMembers() {
        super.refreshedMembers();
        
        ListCanvas canvas = listPanel.getListCanvas();        
        canvas.removeAllNotes();
        for (Enumeration enumeration = getOrderedMemberEnum(); enumeration.hasMoreElements(); )   
        {
            Member m = (Member) enumeration.nextElement();
            if(IAmHidden && m.getName().equals(getMyName()))
            {
                continue;
            }
            canvas.addNote(createMemberNote(canvas, m));
        }
        listPanel.updateBar();
        listPanel.layoutUserPanel();
    }

    @Override
    public void updatedBgimage(Image image) {
        ListCanvas canvas = listPanel.getListCanvas();        
        canvas.setImage(image);
    }

    @Override
    public void selectedNick(String s) {    
        listPanel.updateUserPanel(s);
        super.selectedNick(s);
    }

    @Override
    public void deselectedNick() {
        listPanel.updateUserPanel(null);
        super.deselectedNick();
    }

    public void clickedNote(int i, int j) {
        Chat chat = getChat();
        Member member = getMember(i);
        String s = chat.getSelectedNick();
        ListCanvas canvas = listPanel.getListCanvas();
                
        if (s == null || !s.equalsIgnoreCase(member.getName())) {
            chat.selectNick(member.getName(), getMyChannel(), j);
            canvas.fixNote(i);
        } else if (j == 1) {
            chat.deselectNick();
            canvas.unfixNote();
        }
        if (j >= 2) {
            chat.selectNick(member.getName(), getMyChannel(), j);
        }
    }
}
